<!--
 * @Descripttion: 增加角色
 * @Author: YuZhenJing
 * @Date: 2020-01-12 12:01:01
 * @LastEditors: YuZhenJing
 * @LastEditTime: 2020-03-20 15:25:11
 -->
<template>
  <div class="role-add">
    <tf-dialog
      :title="windowInfo.title"
      :title-icon="windowInfo.titleIcon"
      :visible.sync="tfVisible"
    >
      <template v-slot:content>
        <el-card shadow="never">
          <el-form
            :model="formItem"
            :rules="rules"
            label-width="100px"
            ref="roleForm"
          >
            <el-row :gutter="12">
              <el-col :span="24">
                <el-form-item label="角色ID" prop="roleId">
                  <el-input v-model="formItem.roleId">
                    <template slot="prepend">ROLE_</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="角色名称" prop="roleName">
                  <el-input v-model="formItem.roleName"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="角色类别" prop="roleType">
                  <el-select clearable v-model="formItem.roleType">
                    <el-option
                      :key="item.value"
                      :label="item.name"
                      :value="item.value"
                      v-for="item in roleTypeOptions"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="角色描述" prop="roleDesc">
                  <el-input
                    rows="4"
                    type="textarea"
                    v-model="formItem.roleDesc"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </template>
      <template v-slot:footer>
        <el-row :gutter="12" style="padding:5px;">
          <el-col :span="24" style="text-align:center">
            <el-button
              :loading="saveLoading"
              @click="submitForm"
              icon="el-icon-circle-plus-outline"
              type="primary"
              >保存</el-button
            >
          </el-col>
        </el-row>
      </template>
    </tf-dialog>
  </div>
</template>
<script>
export default {
  components: {
    'tf-dialog': require('@/components/common/dialog/TfDialog').default
  },
  props: {
    visible: {
      type: Boolean,
      required: true,
      default: false
    },
    roleTypeOptions: {
      type: Array,
      required: true
    }
  },
  computed: {
    tfVisible: {
      get() {
        return this.visible
      },
      set(s) {
        // 重置表单
        this.$refs.roleForm.resetFields()
        this.$emit('update:visible', s)
      }
    }
  },
  data() {
    //角色名称唯一性验证
    let roleNameUnique = (rule, value, callback) => {
      const unique = this.$html.get('roles/roleIsUnique/' + value)
      unique
        .then(data => {
          if (data.returnType == 'success') {
            if (data.returnData) {
              return callback()
            } else {
              return callback(new Error('【 ' + value + ' 】已经被占用！'))
            }
          }
        })
        .catch(() => {
          console.log('error:roleNameUnique')
        })
    }
    return {
      // 窗体信息
      windowInfo: {
        titleIcon: 'el-icon-circle-plus-outline',
        title: '新增角色'
      },
      saveLoading: false,
      rules: {
        roleId: [
          { required: true, message: '角色编号不能为空!', trigger: 'blur' },
          {
            pattern: /^[A-Z]*$/,
            message: '角色编号只能为大写字母!',
            trigger: 'blur'
          },
          { max: 36, message: '角色编号不得超过【36】个字符', trigger: 'blur' },
          { validator: roleNameUnique, trigger: 'blur' }
        ],
        roleName: [
          { required: true, message: '角色名称不能为空!', trigger: 'blur' },
          { max: 50, message: '角色名称不得超过【50】个字符', trigger: 'blur' }
        ],
        roleType: [
          { required: true, message: '角色类别不能为空!', trigger: 'change' }
        ],
        roleDesc: [
          { required: true, message: '角色描述不能为空!', trigger: 'blur' },
          { max: 100, message: '角色描述不得超过【50】个字符', trigger: 'blur' }
        ]
      },
      formItem: {
        roleId: '',
        roleName: '',
        roleType: 'execute',
        roleDesc: ''
      }
    }
  },
  methods: {
    // 提交表单保存用户信息
    submitForm() {
      this.$refs.roleForm.validate(valid => {
        if (valid) {
          this.saveLoading = true
          const saveFormItem = this.$html.post(
            'roles/saveNewRole',
            this.formItem
          )
          saveFormItem
            .then(data => {
              if (data.returnType == 'success') {
                this.saveLoading = false
                this.$emit('request')
                this.$notify({
                  title: '成功',
                  message: '保存成功',
                  type: 'success'
                })
                this.tfVisible = false
              }
            })
            .catch(() => {
              this.saveLoading = false
              console.log('error:roleAdd/submitForm')
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>
